/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Algorithms;

/**
 *
 * @author edwin
 */
import java.util.LinkedList;
import java.util.Queue;
import java.util.List;

public class Algorithms {
    
    
    public double AlgorithmSMA(double[] valWeather, int n){        
        Queue<Double> prediction = new LinkedList<>();
    	double sum=0;
    	assert n > 0 : "number of predictions must be > 0";
        for(double x : valWeather){
            sum += x;
            prediction.add(x);
            if(prediction.size() > n){
                sum -= prediction.remove();
            }
        }
        if ( prediction.isEmpty()){
            return 0;
        }
        return sum/prediction.size();
    }
    
    public double AlgorithmDMA(double[] valWeather, int valForecast){
        double sum=0;
        return sum/valForecast;
    }
    
    public double AlgorithmES(double[] valWeather, double alpha){
        assert 1 >= alpha || alpha >= 0: "alpha must be between 1 and 0";
        List<Double> prediction = new LinkedList();
        prediction.add(valWeather[0]);
        for(int i = 1; i < valWeather.length ; i++){
           prediction.add(alpha*valWeather[i]+(1-alpha)*prediction.get(i-1));
        }
        return prediction.get(prediction.size()-1);
    }
    
    public double AlgorithmDES(double[] valWeather, double alpha){
        assert 1 >= alpha || alpha >= 0: "alpha must be between 1 and 0";
        List<Double> p1 = new LinkedList();
        List<Double> p2 = new LinkedList();
        p1.add(valWeather[0]);
        p2.add(valWeather[0]);
        for(int i = 1; i < valWeather.length ; i++){
           p1.add(alpha*valWeather[i]+(1-alpha)*p1.get(i-1));
           p2.add(alpha*p1.get(i)+(1-alpha)*p2.get(i-1));
        }
        int n = p1.size() - 1;
        double A2 = (2* p1.get(n-1) - p2.get(n-1));
        double B2 = (alpha/(1-alpha)* (p1.get(n-1)-p2.get(n-1)));
        return A2 + B2 ;
    }
    
    public double AlgorithmHWM(double[] valWeather, int valForecast){
        double sum=0;
        return sum/valForecast;
    }
    
    
    public double MAPE(double[] At, double[] Ft, int n){
        double mape = 100/n;
        double sum =0;
        for(int t = 0; t<n; t++){
            sum+= Math.abs((At[t] - Ft[t])/(At[t]));
        }
        mape *=sum;
        return mape;
    }
    
}
